/**
 * 预约数据记录选择器
 */

var reserver;

//页面载入初始化
$( "body" ).ready( function( e )
{
	reserver = new ReserveSelector();
	reserver.init();
} );

//窗口大小改变
$( window ).resize( function( e )
{
	if( reserver.visibled() )
	{
		reserver.relocate();
	}
} );

function ReserveSelector()
{
	var html = "<div id='__DIV_RESERVE_SELECTOR_MASK' style='display:block;position:absolute;z-index:10;background-color:#000000;filter:alpha(opacity=20);opacity:0.2;'></div>"
			   + "<div id='__DIV_RESERVE_SELECTOR' class='__DIV_BORDER' style='position:absolute;z-index:10;width:900px;height:500px;'>"
			   + "<dt><span id='title'>预约记录查询</span><a id='btn_cancel'>[ 取消 ]</a> <a id='btn_enter'>[ 确定 ]</a></dt>"
			   + "<div id='form'>"
			   + "<p><label>客户名字：</label><input type='text' id='client_ns' size='15' placeholder='客户名字' />"
			   + "<label>测评日期：</label><input type='text' id='rep_date' value='' size='15' maxlength='15' rel='DATE::Y-m-d' readonly='readonly' class='DatePicker_Icon'/>"
			   + "<label>预约时间：</label><select id='time'><option value=''>-- 请选择 --</option>"
			   + "<option value='09:30'>09:30</option>" +
			   		"<option value='10:00'>10:00</option>" +
			   		"<option value='10:30'>10:30</option>" +
			   		"<option value='11:00'>11:00</option>" +
			   		"<option value='11:30'>11:30</option>" +
			   		"<option value='12:00'>12:00</option>" +
			   		"<option value='12:30'>12:30</option>" +
			   		"<option value='13:00'>13:00</option>" +
			   		"<option value='13:30'>13:30</option>" +
			   		"<option value='14:00'>14:00</option>" +
			   		"<option value='14:30'>14:30</option>" +
			   		"<option value='15:00'>15:00</option>" +
			   		"<option value='15:30'>15:30</option>" +
			   		"<option value='16:00'>16:00</option>" +
			   		"<option value='16:30'>16:30</option>" +
			   		"<option value='17:00'>17:00</option>" +
			   		"<option value='17:30'>17:30</option>" +
			   		"<option value='18:00'>18:00</option>" +
			   		"<option value='18:30'>18:30</option>" +
			   		"<option value='19:00'>19:00</option>" +
			   		"<option value='19:30'>19:30</option>" +
			   		"</select>"
			   + "<label>测评中心：</label><select id='region'>" +
			   		"<option value=''>--请选择--</option>"+
			   		"<option value='2cqn3yqhd76m'>北京知春路中心</option>" +
			   		"<option value='3qmqoulejghk'>北京首师大中心</option>" +
			   		"<option value='0bji8aq5mcyw'>北京崇文门中心</option>" +
			   		"<option value='dc9nakhso82n'>北京工体中心</option>" +
			   		"<option value='wft40d5cd0uo'>北京望京中心</option>" +
			   		"<option value='2vstsfg33a5i'>北京通州中心</option>" +
			   		"<option value='wi947px27op1'>上海隆德路中心</option>" +
			   		"<option value='itb6y9h6o7xd'>上海东方路中心</option>" +
			   		"<option value='5g0s32muir51'>上海张杨路中心</option>" +
			   		"<option value='1az1x4vtlq79'>上海徐汇中心</option>" +
			   		"<option value='opsgz7uy0fnf'>上海虹口中心</option>" +
			   		"<option value='svah5207yg3i'>杭州学院路中心</option>" +
			   		"<option value='c9qbtw1fzvc6'>杭州景芳中心</option>" +
			   		"<option value='3jbzzytgqb79'>杭州武林广场中心</option>" +
			   		"<option value='za4ra1a5rmnh'>杭州滨和路中心</option>" +
			   		"<option value='kstn444vljz3'>深圳世界之窗中心</option>" +
			   		"<option value='y3yj89tj2xwy'>深圳大剧院中心</option>" +
			   		"<option value='81gemift3w4a'>南京南大中心</option>" +
			   		"<option value='m6439meecp1f'>南京雨花台中心</option>" +
			   		"<option value='gpqeb0k6huw8'>天津天大中心</option>" +
			   		"<option value='q7rf3yu0jdmj'>天津滨海中心</option>" +
			   		"<option value='hh11u54kv1hl'>天津小白楼中心</option>" +
			   		"<option value='m1w9eyd7iutg'>广州区庄中心</option>" +
			   		"<option value='r6eb4ihewvpw'>广州华师中心</option>" +
			   		"<option value='y435f98w04b6'>广州昌岗中心</option>" +
			   		"<option value='uux9spk4i5r5'>苏州广济南路中心</option>" +
			   		"<option value='s1x3qoji5vbu'>苏州园区中心</option>" +
			   		"<option value='rshj5c0q553u'>宁波鄞州中心</option>" +
			   		"<option value='7t3rw3cdzgso'>无锡五爱路中心</option>" +
			   		"<option value='tlm0sw0secdv'>武汉中南路中心</option>" +
			   		"<option value='gg86w54k12l6'>武汉循礼门中心</option>" +
			   		"<option value='6i9x47xk7r2j'>长沙五一大道中心</option>" +
			   		"<option value='6xulzwf2p148'>重庆观音桥中心</option>" +
			   		"<option value='byivmjaxsfq6'>成都骡马市中心</option></select></p>"
			   + "<span class='caption'>（按回车键进行查询）</span>"
			   + "</div>" + "<div id='body'><ul id='reserves'></ul></div>" + "</div>";
	
	var $mask;
	var $box;
	var $body;
	var $list;
	var $form;
	var callback;
	var tmpData;// 临时查询的数据集合

	var selectedId;
	var _moveable = false;

	this.init = function()
	{
		
		$( "body" ).append( html );
		$box = $( "#__DIV_RESERVE_SELECTOR" );
		$mask = $( "#__DIV_RESERVE_SELECTOR_MASK" );
		$body = $box.find( "#body" );
		$form = $box.find( "#form" );
		$list = $body.find( "ul" );
		
		$form.find("#rep_date" ).Zebra_DatePicker(
				{ format: 'Y-m-d', show_icon: true, onSelect: birthday_changed} );
		
		empty_list();
		
		$box.find( "dt" ).bind( {
			mousedown: function( e )
			{
				_moveable = true;
				// 鼠标相对于当前窗口的位置
				var offsetX = e.pageX - $box.x();
				var offsetY = e.pageY - $box.y();

				$( "body" ).bind( "mousemove", {
					ox: offsetX,
					oy: offsetY
				}, function( e )
				{
					if( _moveable )
					{
						$box.move( e.pageX - e.data.ox, e.pageY - e.data.oy );
					}
				} );
			
			},
			mouseup: function( e )
			{
				_moveable = false;
				$( "body" ).unbind( "mousemove" );
			}
		} );

		$box.find( "#btn_cancel" ).click( {
			cur: this
		}, function( e )
		{
			e.stopPropagation();
			var pcur = e.data.cur;
			pcur.close();
		} );
		$box.find( "#btn_enter" ).click( {
			cur: this
		}, function( e )
		{
			e.stopPropagation();
			var pcur = e.data.cur;
			reserve_click();
			pcur.close();
		} );

		$form.find( "#client_ns" ).keyup( form_input_keyupHandler );
		$form.find( "#rep_date" ).change( data_submit );
		$form.find( "#time" ).change( data_submit );
		$form.find( "#region" ).change( data_submit );
		
		$box.hide();
		$mask.hide();
	};
	
	function birthday_changed()
	{
		data_submit();
	}
	
	function empty_list()
	{
		$list.empty();
		$list.append( "<li class='empty'>请输入搜索条件，符合条件的结果在此处列出。<br><br><p class='bold'>筛选原则：</p>"
					  + "<p>（1）<span class='bold'>作为接待人</span> 只有自己是“预约接待人”的预约记录才符合。</p>"
					  + "<p>（2）<span class='bold'>客户所有权</span> 客户资源必须属于自己，否则请相关人员进行转交操作。</p>"
					  + "<p>（3）<span class='bold'>未接待</span> 必须是未相应添加过接待的预约才会被列出。</p>"
					  + "<p><br><span class='bold'>提醒：</span> 条件内容都是模糊查询，可不用写全。</p>" + "</li>" );
	};

	function form_input_keyupHandler( e )
	{
		if( e.which == 13 )
		{
			data_submit();
		}
	}

	function data_submit()
	{
		var nickname = $form.find( "#client_ns" ).val();
		var rep_date = $form.find( "#rep_date" ).val();
		var time = $form.find( "#time" ).val();
		var region = $form.find( "#region" ).val();
		var param = {};
		var val_valid = false;

		if( !empty( nickname ) )
		{
			val_valid = true;
			param.nickname = nickname;
		}

		if( !empty( rep_date ) )
		{
			val_valid = true;
			param.rep_date = rep_date;
		}

		if( !empty( time ) )
		{
			val_valid = true;
			param.time = time;
		}

		if( !empty( region ) )
		{
			val_valid = true;
			param.region = region;
		}

		if( !val_valid )
		{
			empty_list();
		}
		else
		{
			$.post(contextPath+"/index/consultant_reserve_user.json", param, function( d )
			{
				tmpData = d.list;
				$list.find( "li" ).unbind( "click" );
				$list.empty();
				var list = d.list
				for( var i = 0; i < list.length; i++ )
				{
					var v = list[ i ];
					var phone = empty( v.phone ) ? "无" : v.phone;
					var qq = empty( v.qq ) ? "无" : v.qq;
					var email = empty( v.email ) ? "无" : v.email;
					var address = v.province + " - " + v.city;

					var note;

					if( v.state == 3 )
					{
						note = "<span style='color:#bbbbbb;'>（已过期）</span>";
					}
					else if(v.state == 1)
					{	
						note = "<span style='color:#99cc00;'>（待确认）</span>";
					}
					else if(v.state == 2){
						note = "<span style='color:#99cc00;'>（已出现）</span>";
					}

					var type = "";

					if( v.non == 1 )
					{
						type = "<p class='__RESERVE_INFO_LIST'><span style='color:#bbbbbb;'><b>预约类型：</b>非测评（"
							   + v.notice + "）</span></p>";
					}
					else
					{
						type = "<p class='__RESERVE_INFO_LIST'><span style='color:#ff3300;'><b>预约类型：</b>测评预约</span></p>";
					}

					$list.append( "<li id="+v.client+" name="+v.keyname+" rel='" 
								  + i
								  + "'>" //
								  + ( i + 1 )
								  + "、"
								  + "<span name="+v.dates+" class='stress'>"
								  + v.nickname
								  + "</span>"//

								  + ( v.invalid == 1 ? "<em class='caption'>（无效客户）</em>"
							: "<em class='valid'>（有效客户）</em>" )

								  + "<p class='__RESERVE_INFO_LIST'>"//
								  + "<span><b>预约人：</b>" + v.signer + "</span>"//
								  + "<span><b>预约日期：</b>" + v.dates+ " " + v.time + note + "</span>"//
								  + "<span><b>登记日期：</b>" + v.startTime + "</span>"//
								  + "</p>"

								  + type //

								  + "<p class='__RESERVE_INFO_LIST'>"//
								  + "<span><b>渠道：</b>" + v.sourcens + "</span>"//
								  + "<span><b>类别：</b>" + v.categoryns + "</span>"//
								  + "</p>"

								  + "<p class='__RESERVE_INFO_LIST'>"//
								  + "<span><b>地址：</b>" + address + "</span>"//
								  + "</p>"

								  + "<p class='__RESERVE_INFO_LIST'>"//
								  + "<span><b>电话：</b>" + phone + "</span>"//
								  + "<span><b>QQ：</b>" + qq + "</span>"//
								  + "<span><b>邮箱：</b>" + email + "</span>"//
								  + "</p>"

								  + "</li>" );


					$list.find( "li" ).bind( {
						click: itemClickHandler,
						dblclick: itemClickHandler
					} );

					$list.find( "li" ).each( function()
					{
						disableSelect( this );
						$( this ).find( "p" ).mousedown( function()
						{
							return false;
						} );
					} );
				}
			}, "json" );
		}
	}


	function itemClickHandler( e )
	{
		e.stopPropagation();
		e.stopImmediatePropagation();
		var lastId = parseInt( $list.find( "li.selected" ).attr( "rel" ) );
		var curId = parseInt( $( this ).attr( "rel" ) );
		$list.find( "li" ).removeClass( "selected" );

		if( lastId != curId )
		{
			selectedId = curId;
			$( this ).addClass( "selected" );
		}
		else
		{
			selectedId = -1;
		}

		if( e.type == "dblclick" )
		{
			if( $.isFunction( callback ) && tmpData != null && selectedId != -1 )
			{
				callback( tmpData[ selectedId ] );
			}
			reserver.close();
		}
	}

	function reserve_click(){
		var id = $("#reserves").find(".selected").attr("id");
		var keyname = $("#reserves").find(".selected").attr("name");
		var resdate = $("#reserves").find(".stress").attr("name"); 
		$.post(contextPath+"/index/getclientall.json", {
				"client":id,"keyname":keyname,"resdate":resdate
			}, function(data) {
				$("#username").hide();
				var ulist = data.uList;
				var offlist = data.offList;
				var client = data.client;
				var stulist = data.stuList;
				var user = data.user;
				var use1List = data.use1List;
				var useList = data.useList;
				var use2List = data.use2List;
				var phone ="";
				if(client!=null){
					var youwu="";
					if(client.invalid=="0"){
						youwu="有效客户";
					}else{
						youwu="无效客户";
					}
					
					var ph = client.phlist;
					if(ph!=null){
					for(var j=0;j<ph.length;j++){
						phone = ph[j].phone;
					}
					}else{
						phone = client.phone;
				  }
					$("#nickname").html(client.nickname+'<em class="valid">'+youwu+'</em>');
					$("#nickname").attr("name",client.keyname);
					$("#reskey").val(data.reskey);
 					$("#source").html(client.sourcens);
					$("#keyword").html(client.keyword);
					$("#category").html(client.categoryns)
					$("#phone").html(phone);
					$("#address").html(client.address);
					$("#email").html(client.email);
					$("#qq").html(client.qq);
					$("#ip").html(client.ip);
					$("#desc").html(client.descs);
					
				}
				var htm = $("#student_test_list").html(" ");
				
				var offhtml = "";
				offhtml = '<select id="region" onchange="offChange(this.value)">'
					offhtml += '<option value="">-- 请选择 --</option>' 
				for(var h=0;h<offlist.length;h++){
					if(user.region == offlist[h].keyname){
					offhtml +='<option value="'+offlist[h].keyname+'" selected>'+offlist[h].name+'</option>'
					}else{
						offhtml +='<option value="'+offlist[h].keyname+'">'+offlist[h].name+'</option>'
					}
				}
				offhtml +='</select>';
				$("#rep_region_box").html(offhtml);
				
				
				getTeacher(user.region);
				var uhtml = "";
				uhtml += '<select name="target" id="target">'
				uhtml += '<option value="">-- 请选择目标 --</option>'
				for(var h=0;h<ulist.length;h++){
					if(user.username == ulist[h].username){
						uhtml +='<option  value="'+ulist[h].username+'" selected>'+ulist[h].nickname+'（'+ulist[h].regions+'-'+ulist[h].departments+'-'+ulist[h].job+'）</option>';
					}else{
						uhtml +='<option  value="'+ulist[h].username+'">'+ulist[h].nickname+'（'+ulist[h].regions+'-'+ulist[h].departments+'-'+ulist[h].job+'）</option>';
					}
				}
				uhtml +='</select>';
				$("#rep_user_box").html(uhtml);
				
				worths();			   
		
				var Objhtml = "";
					Objhtml = '<ul id="rep_student">'
				for(var i=0;i<stulist.length;i++){
					Objhtml += '<li rel="'+stulist[i].keyname+'"   onclick="check('+stulist[i].id+')" id="'+stulist[i].id+'"  class="stress rect" style="list-style:none;float:left;width:200px;margin:2px;">'+
					   '<label><input type="checkbox" checked id="'+stulist[i].keyname+'" rel="'+stulist[i].keyname+'">'+
					   '<span>'+stulist[i].nickname+'  ，'+stulist[i].sex+' ， '+stulist[i].age+' 岁</span>'+
					   '</label></li>'
					setStudentTest(stulist[i].keyname);
				}
					Objhtml +='</ul>'
				$("#rep_student_box").html(Objhtml);
				
				if(user!==null){
					$("#user_nickname").html(user.nickname);
					$("#date").val($("#dates").val());
					$("#time").val($("#times").val());
				}
		});
	}
	
	function setStudentTest(d){
		$("#student_test_list").show();
	    ClientReception.prototype.setStudentTestData(d);
	    }
		
	// 是否显示
	this.visibled = function()
	{
		return $box.visible();
	};

	// 显示单条信息
	this.open = function()
	{
		this.relocate();
		$box.fadeIn( 150 );
		$mask.fadeIn( 150 );
	};

	this.reset = function()
	{
		selectedId = -1;
		tmpData = null;

		$form.find( "#client_ns" ).val( "" );
		$form.find( "#reserve_ns" ).val( "" );
		$form.find( "#address" ).val( "" );
		$form.find( "#phone" ).val( "" );
		$form.find( "#email" ).val( "" );
		$form.find( "#qq" ).val( "" );
		$form.find( "#type_ns" ).val( "" );

		empty_list();
	};

	this.relocate = function()
	{
		var bw = $( "body" ).width();
		var bh = $( "body" ).height();
		var w = $box.width();
		var h = $box.height();

		$mask.width( bw + 5 );
		$mask.height( bh );
		$mask.move( 0, 0 );
		$box.move( ( bw - w ) / 2, ( bh - h ) / 2 * 0.8 );
	};

	this.close = function()
	{
		$box.fadeOut( 150 );
		$mask.fadeOut( 150 );
	};

	this.setCallback = function( fn )
	{
		callback = fn;
	};
	
	
}
